package com.atguigu.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.io.IOException;

/**
 * @author yhm
 * @create 2023-09-18 14:23
 */
public class HBaseConnection {

    private static Connection connection = null;
    static {
        // 创建连接配置对象
//        Configuration conf = new Configuration();
//
//        conf.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
        // 创建同步连接
        try {
            connection = ConnectionFactory.createConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Connection getHBaseConnection(){
        if (connection != null){
            return connection;
        }else {
            throw new RuntimeException("获取HBase连接异常");
        }
    }

    public static void closeHBaseConnection() throws IOException {
        if(connection != null && !connection.isClosed()){
            connection.close();
        }
    }

    public static void main(String[] args) throws IOException {
        // 创建连接配置对象
        Configuration conf = new Configuration();

        conf.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");

        // 创建同步连接
        Connection connection = ConnectionFactory.createConnection(conf);

        // 打印
        Admin admin = connection.getAdmin();
        System.out.println(admin);


        // 关闭释放资源
        connection.close();
    }
}
